﻿using UnityEngine;
using System.Collections;

public class ShapeScript : MonoBehaviour {

	Vector3 initialPosition, destination;
	Material defaultMat;
	int id;
	public Vector3[] cubes;
	public bool multiCube = false;
	public bool regularMultiCube = false;

	void Start () {
		initialPosition = transform.position;
		defaultMat = renderer.material;
		if (!multiCube && !regularMultiCube) {
			cubes = new Vector3[1];
			cubes [0] = new Vector3 (Mathf.RoundToInt (transform.position.x), Mathf.RoundToInt (transform.position.y), Mathf.RoundToInt (transform.position.z));
		}
	}

	public void setId(int id){
		this.id = id;
	}

	public int getId(){
		return id;
	}
	
	public Vector3 getInitialPosition () {
		return initialPosition;
	}

	public void setInitialPosition (Vector3 initialPosition) {
		this.initialPosition = initialPosition;
	}

	public void setDestination(Vector3 puzzleCenter){
		this.destination = initialPosition + ((initialPosition-puzzleCenter)*2);
	}

	public Vector3 getDestination(){
		return destination;
	}

	public bool canCollapse(Vector3 direction){
		RaycastHit hit;
		Ray ray = new Ray (direction, transform.position);
		if (Physics.Raycast (ray, out hit, 1.0f)) {
			//Debug.DrawRay (transform.position, direction, Color.blue, 1);
		}
		if (hit.point.x == 0 && hit.point.y == 0) {
			Debug.Log ("FALL");
			return true;
		} else {
			return false;
		}
	}

	public void OnMouseOver(){
		renderer.material = transform.parent.GetComponent<PuzzleScript> ().highlight;
		if(Input.GetButtonDown("Fire3")){
			transform.parent.GetComponent<GridScript> ().removeShape(id);
			Debug.Log(transform.position);
			Destroy(gameObject);
			//transform.parent.GetComponent<ExpandScript> ().checkChildren();
		}
	}

	public void OnMouseExit(){
		renderer.material = defaultMat;
	}

	public void updateLocation(Vector3 puzzleCenter){
		//initialPosition = transform.position;
		this.destination = initialPosition + ((initialPosition-puzzleCenter)*2);
	}
}
		